﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Text;
using System.IO;
using System.Configuration;
namespace AplicacionTest.Web.AppCode
{
    public static class SoporteUI
    {
        public static string GetConnectionStringConfig(string key)
        {
            return ConfigurationManager.ConnectionStrings[key].ConnectionString.ToString().Trim();
        }

        public static string RootFileServerUser(string keyFileServer, string keyPrefix,string archivo)
        {
            string rootFileServer = SoporteUI.GetConnectionStringConfig(keyFileServer);

            return Path.Combine(rootFileServer, keyPrefix, archivo);
        }

        public static bool ExportarExcelFileServer(HttpResponse response,string keyFileServer,string keyPrefix, string archivo)
        {
            string root = SoporteUI.RootFileServerUser(keyFileServer, keyPrefix, archivo);

            using (StreamReader notePad = new StreamReader(root, Encoding.Default))
            {
                response.Clear();
                response.Buffer = true;
                response.AddHeader("content-disposition", "attachment;filename=" + archivo);
                response.Charset = "ISO-8859-1";
                response.ContentEncoding = System.Text.Encoding.Default;
                response.ContentType = "application/vnd.ms-excel";
                response.Write(notePad.ReadToEnd());

                notePad.Close();
                notePad.Dispose();

                response.End();
            }

            return true;
        }

        public static string FileTableHTMLGridJSON(IList dataTable,List<GridJSONField> fields,string keyFileServer, string keyPrefix,string archivo,string extension)
        {
            string rootFileServer = Path.Combine(SoporteUI.GetConnectionStringConfig(keyFileServer), keyPrefix);

            if (!Directory.Exists(rootFileServer)) Directory.CreateDirectory(rootFileServer);

            archivo = archivo + "." + extension;

            string root = SoporteUI.RootFileServerUser(keyFileServer, keyPrefix, archivo);

            using (StreamWriter notePad = new StreamWriter(root, false, Encoding.Default))
            {
                notePad.WriteLine("<style> ");
                notePad.WriteLine("   .TableGridJSON {width:100%;padding: 0px 0px 0px 0px;margin:0px 0px 0px 0px;border-spacing:0;border-collapse:collapse;}");
                notePad.WriteLine("   .TableGridJSON tr th {background-color: #E6E6E6;color: #413D3A;height:15px;padding: 5px 0px 5px 10px;text-align:center;cursor:default;font-size:10px;}");
                notePad.WriteLine("   .TableGridJSON tr td {color:#375934;height:8px;padding: 5px 0px 5px 10px;text-align:left;font-size:11px;background-color : #DEFDDD;}");
                notePad.WriteLine("   .rowGridJSON {background-color : #DEFDDD;}");
                notePad.WriteLine("   .rowGridJSONAlter{ background-color : #EEFFED;}");
                notePad.WriteLine("</style> ");
                notePad.WriteLine("");
                notePad.WriteLine("<table class='TableGridJSON' border='1' > ");
                notePad.WriteLine("   <tr>");

                foreach (GridJSONField campo in fields)
                    notePad.WriteLine("       <th>" + campo.Header + "</th>");

                notePad.WriteLine("   </tr>");

                bool flagAlternate = false;
                string classRowGrid = null;

                foreach (Object e in dataTable)
                {
                    classRowGrid = (!flagAlternate ? "rowGridJSON" : "rowGridJSONAlter");

                    flagAlternate = !flagAlternate;

                    notePad.WriteLine("   <tr class='" + classRowGrid + "'> ");

                    foreach (GridJSONField campo in fields)
                        notePad.WriteLine("       <td>" + e.GetType().GetProperty(campo.DataField).GetValue(e, null).ToString().Trim() + "</td> ");

                    notePad.WriteLine("   </tr> ");
                }

                notePad.WriteLine("</table>");
            }

            return archivo;
        }
    }
}